import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:frontend/models/address.dart';
import 'package:frontend/pages/addressEditPage.dart';
import 'package:frontend/states/NetState.dart';
import 'package:provider/provider.dart';

import '../models/global.dart';
import '../reqs/requests.dart';
import '../widgets/expandableFab.dart';

class AddressPage extends StatefulWidget {
  const AddressPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return AddressPageState();
  }
}

class AddressPageState extends State<AddressPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //automaticallyImplyLeading: false,
        title: Text("地址管理"),
      ),
      body: Consumer<NetState>(builder: (context, netState, widget) {
        return FutureBuilder<List<Address>>(
            future: fetchAddresses(Global.profile.user!.id),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                if (snapshot.hasError) {
                  return Text(snapshot.error.toString());
                }
                return Scrollbar(
                  child: ListView(
                    restorationId: 'list_demo_list_view',
                    padding: const EdgeInsets.symmetric(vertical: 8),
                    children: [
                      for (var address in snapshot.data!)
                        ListTile(
                            leading: Text(address.tag),
                            title: Text(
                                '${address.contact} ${address.phone_number}'),
                            subtitle: Text(address.address),
                            trailing: FittedBox(
                              fit: BoxFit.scaleDown,
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  TextButton(
                                    onPressed: () {
                                      Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) =>
                                                  AddressEditPage(
                                                    address: address,
                                                  )));
                                    },
                                    child: Text('编辑',),
                                  ),
                                  TextButton(
                                      onPressed: () {
                                        showDialog<void>(
                                          context: context,
                                          builder: (context) {
                                            return AlertDialog(
                                              content: Text("删除地址？"),
                                              actions: [
                                                TextButton(
                                                  onPressed: () {
                                                    delAddress(
                                                        address.address_id);
                                                    ScaffoldMessenger.of(context)
                                                        .showSnackBar(
                                                      const SnackBar(
                                                        content: Text('地址已删除！'),
                                                      ),
                                                    );
                                                    Provider.of<NetState>(context,
                                                            listen: false)
                                                        .somethingChangedInBackend();
                                                    Navigator.of(context).pop();
                                                  },
                                                  child: const Text('确认'),
                                                ),
                                                TextButton(
                                                  onPressed: () =>
                                                      Navigator.of(context).pop(),
                                                  child: const Text('取消'),
                                                ),
                                              ],
                                            );
                                          },
                                        );
                                      },
                                      child: Text('删除',)),
                                ],
                              ),
                            )),
                    ],
                  ),
                );
              } else {
                return const CircularProgressIndicator();
              }
            });
      }),
      floatingActionButton: ExpandableFab(
        distance: 112.0,
        children: [
          ActionButton(
            icon: Icon(Icons.refresh),
            onPressed: () => setState(() {}),
          ),
          ActionButton(
            icon: Icon(Icons.add),
            onPressed: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => AddressEditPage()));
            },
          ),
          ActionButton(icon: Icon(Icons.check_box_outline_blank)),
        ],
      ),
    );
  }
}
